package face.pay.common.quota.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import face.pay.common.quota.service.IQuotaInfoService;
import face.pay.dao.AgencyLevelDao;
import face.pay.dao.AgencyQuotaApplyDao;
import face.pay.dao.AgencyQuotaDao;
import face.pay.entity.AgencyQuota;
import face.pay.entity.AgencyQuotaApply;
import face.pay.entity.UserInfo;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: zkj
 * @Date: 2019/7/17 13:43
 * @Description:
 */
@Service
@Slf4j
@Transactional
public class QuotaInfoServiceImpl implements IQuotaInfoService {

    @Autowired
    AgencyQuotaDao agencyQuotaDao;

    @Autowired
    AgencyQuotaApplyDao agencyQuotaApplyDao;

    @Autowired
    AgencyLevelDao agencyLevelDao;

    /**
     * 当前用户 配额
     * @param loginUser
     * @param pageParam
     * @return
     */
    @Override
    public PageData queryUserData(UserInfo loginUser, PageParam pageParam) {

        AgencyQuota agencyQuota  = new AgencyQuota();

        agencyQuota.setOemId(loginUser.getOemId());

        agencyQuota.setUserId(loginUser.getId());

        PageHelper.startPage(pageParam.getPageNo(),pageParam.getPageSize());


        List<AgencyQuota> agencyQuotas = this.agencyQuotaDao.queryUserQuota(loginUser);


        PageInfo<AgencyQuota> pageInfo = new PageInfo <AgencyQuota>(agencyQuotas);

        PageData<AgencyQuota> pageData = new PageData<AgencyQuota>(pageInfo.getTotal(),agencyQuotas);

        return pageData;
    }

    /**
     * 商户发起
     * @param agencyQuotaApply
     * @param loginUser
     * @return
     */
    @Override
    public ResultMessage startApply(AgencyQuotaApply agencyQuotaApply, UserInfo loginUser) {

        log.info("代理商发起 购买额度申请");
        agencyQuotaApply.setApplyUserId(loginUser.getId());
        agencyQuotaApply.setCreatedBy(loginUser.getId());
        agencyQuotaApply.setType(AgencyQuotaApply.TYPE.NORMAL);
        agencyQuotaApply.setOemId(loginUser.getOemId());
        agencyQuotaApply.setStatus(0);
        agencyQuotaApplyDao.insert(agencyQuotaApply);

        return ResultMessage.createSuccess();
    }


   }
